﻿using AutoMapper;
using Common.Communal;
using MediatR;
using Product.API.ISerivces.Material;
using Product.API.ISerivces.Products;
using Product.API.Write.Applications.Commands.Material;

namespace Product.API.Write.Applications.CommandHandlers.Material
{
    /// <summary>
    /// 物料删除（逻辑）命令处理
    /// </summary>
    public class DeleteMaterialCommandHandler : IRequestHandler<DeleteMaterialCommand, ApiResult<int>>
    {
        /// <summary>
        /// 物料仓储
        /// </summary>
        private readonly IMaterialSerivces materialSerivces;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="materialSerivces">物料仓储</param>
        /// <param name="mapper">轮廓映射</param>
        public DeleteMaterialCommandHandler(IMaterialSerivces materialSerivces)
        {
            this.materialSerivces = materialSerivces;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public async Task<ApiResult<int>> Handle(DeleteMaterialCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code = EnumCode.Ok;
            result.Message = EnumCode.Ok.ToString();

            int data = await materialSerivces.DeleteMaterial(request.Id);
            result.Data = data;

            return await Task.FromResult(result);
        }
    }
}
